// 给定一个链表的头节点head，判断链表是否有环

// 思路：快慢指针，快指针每次2步，慢指针每次1步，如果有环的话，快指针入环后最终会在某个节点和慢指针相遇
// 时间复杂度：O(n)
// 空间复杂度：O(1)

const { LinkedList, ListNode} = require('./64.设计链表')

function hasCycle(head) {
    if (!head || !head.next) {
        return false
    }
    let slow = head.next
    let fast = head.next.next
    while (slow !== fast) {
        if (!fast || !fast.next) {
            return false
        }
        slow = slow.next
        fast = fast.next.next
    }
    return true
}